package com.smartStatus.security.comment;

import cn.hutool.core.util.StrUtil;
import com.smartStatus.core.constant.SecurityConstant;
import com.smartStatus.security.annoation.Ignore;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.runtime.Inner;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.security.access.AccessDeniedException;
import sun.security.util.SecurityConstants;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName : StatusSecurityIgnore
 * @Author : lby
 * @Date: 2021/5/6 21:38
 * @Description : ignore security 没有权限
 */
@Slf4j
@Aspect
@AllArgsConstructor
public class StatusSecurityIgnore {
    private final HttpServletRequest request;

    @SneakyThrows
    @Around("@annotation(ignore)")
    public Object around(ProceedingJoinPoint point, Ignore ignore) {
        String header = request.getHeader(SecurityConstant.FROM);
        if (ignore.value() && !StrUtil.equals(SecurityConstant.FROM_IN, header)) {
            log.warn("访问接口 {} 没有权限", point.getSignature().getName());
            throw new AccessDeniedException("Access is denied");
        }
        return point.proceed();
    }
}
